//
//  StatusTableViewCell.swift
//  WeiBoOfSwift
//
//  Created by aven wu on 26/04/2018.
//  Copyright © 2018 GML. All rights reserved.
//

import UIKit
import SDWebImage

/**
 保存cell的重用标示
 
 - NormalCell:  原创微博的重用标识
 - ForwardCell: 转发微博的重用标识
 */
enum StatusTableViewCellIdentifier: String
{
    case NormalCell = "NormalCell"
    case ForwardCell = "ForwardCell"
    
    // 如果在枚举中利用static修饰一个方法 , 相当于类中的class修饰方法
    // 如果调用枚举值的rawValue, 那么意味着拿到枚举对应的原始值
    static func cellID(status: Status) ->String
    {
        return status.retweeted_status != nil ? ForwardCell.rawValue : NormalCell.rawValue
        
    }
}

protocol StatusTableViewCellDelegate:NSObjectProtocol {
    func statusTableViewCell(cell:StatusTableViewCell,index:Int,picUrl:[NSURL])
}

class StatusTableViewCell: UITableViewCell {

   weak var cellDelegate:StatusTableViewCellDelegate?
    
    var status:Status?
    {
        // 类似OC中重写setter方法
        didSet{
            iconView.setImageWith((status?.user?.profile_image_URL)! as URL)
            nameLabel.text = status?.user?.name
            timeLabel.text = status?.created_at
            sourceLabel.text = status?.source
            contentLabel.text = status?.text
            // 设置认证图标
            verifiedView.image = status?.user?.verifiedImage

            // 设置会员图标
            vipView.image = status?.user?.mbrankImage
            
            // 计算微博配图
            // 设置配图的尺寸
            pictureView.status = status?.retweeted_status != nil ? status?.retweeted_status :  status
            
            let picViewSize = pictureView.calculateImageSize()
            pictureView.snp.updateConstraints { (make) -> Void in
                make.width.equalTo(picViewSize.width)
                make.height.equalTo(picViewSize.height)
            }
            
        }
    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /**
     用于获取行号
     */
    func rowHeight(status: Status) -> CGFloat
    {
        // 1.为了能够调用didSet, 计算配图的高度
        self.status = status
        
        // 2.强制更新界面
        self.layoutIfNeeded()
        
        // 3.返回底部视图最大的Y值
        return footerView.frame.origin.y+footerView.frame.size.height
    }
    
     func setupUI()
    {
        // 1.添加子控件
        contentView.addSubview(iconView)
        contentView.addSubview(verifiedView)
        contentView.addSubview(nameLabel)
        contentView.addSubview(vipView)
        contentView.addSubview(timeLabel)
        contentView.addSubview(sourceLabel)
        contentView.addSubview(contentLabel)
        contentView.addSubview(footerView)
        contentView.addSubview(pictureView)

        // 2 布局子控件
        let marginLeftX = 10*KTC_SCREEN_RATION
        iconView.snp.makeConstraints { (make) -> Void in
            make.left.equalTo(marginLeftX)
            make.top.equalToSuperview().offset(marginLeftX)
            make.width.equalTo(40)
            make.height.equalTo(40)
        }
        iconView.layer.cornerRadius = 20
        
        verifiedView.snp.makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.iconView.snp.right)
            make.centerY.equalTo(self.iconView.snp.bottom)
        }
        nameLabel.snp.makeConstraints { (make) -> Void in
            make.left.equalTo(self.iconView.snp.right).offset(marginLeftX)
            make.top.equalTo(self.iconView)
            make.right.equalToSuperview().offset(marginLeftX)
        }
        vipView.backgroundColor = UIColor.randomColor
        vipView.snp.makeConstraints { (make) -> Void in
            make.left.equalTo(self.nameLabel.snp.right)
            make.bottom.equalTo(self.nameLabel.snp.bottom)
            make.width.height.equalTo(10)
        }
        timeLabel.snp.makeConstraints { (make) -> Void in
            make.left.equalTo(self.nameLabel)
            make.bottom.equalTo(self.iconView)
        }
        sourceLabel.snp.makeConstraints { (make) -> Void in
            make.left.equalTo(self.timeLabel.snp.right).offset(marginLeftX)
            make.centerY.equalTo(self.timeLabel.snp.centerY)
        }
        contentLabel.snp.makeConstraints { (make) -> Void in
            make.left.equalTo(self.iconView)
            make.top.equalTo(self.iconView.snp.bottom).offset(marginLeftX)
            make.right.lessThanOrEqualTo(self.contentView).offset(marginLeftX)
        }
        
        pictureView.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(self.contentLabel.snp.bottom).offset(marginLeftX)
            make.left.equalTo(self.contentView.snp.left).offset(marginLeftX)
            make.width.equalTo(0)
            make.height.equalTo(0)
            
        }
        

        footerView.snp.makeConstraints { (make) -> Void in
            make.left.equalToSuperview()
            make.top.equalTo(self.pictureView.snp.bottom).offset(marginLeftX)
            make.width.equalTo(UIScreen.main.bounds.size.width)
            make.height.equalTo(44)
        }
        
    }
    
    
    // MARK: - 懒加载
    /// 头像
    lazy var iconView: UIImageView =
    {
        let iv = UIImageView(image: UIImage(named: "avatar_default_big"))
        iv.clipsToBounds = true
        return iv
    }()
    /// 认证图标
    private lazy var verifiedView: UIImageView = UIImageView(image: UIImage(named: "avatar_enterprise_vip"))
    
    /// 昵称
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.darkGray
        label.font = UIFont.systemFont(ofSize: 14)
        return label
    }()
    /// 会员图标
    private lazy var vipView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    
    /// 时间
    private lazy var timeLabel: UILabel =
    {
        let label = UILabel()
        label.textColor = UIColor.darkGray
        label.font = UIFont.systemFont(ofSize: 12)
        return label
    }()
    /// 来源
    private lazy var sourceLabel: UILabel =
    {
        let label = UILabel()
        label.textColor = UIColor.darkGray
        label.font = UIFont.systemFont(ofSize: 12)
        
        return label
    }()
    
    /// 正文
    lazy var contentLabel: UILabel =
    {
        let label = UILabel()
        label.textColor = UIColor.darkGray
        label.numberOfLines = 0
        label.font = UIFont.systemFont(ofSize: 15)
        label.preferredMaxLayoutWidth = UIScreen.main.bounds.width - 20
        return label
    }()
    /// 配图
    lazy var pictureView: StatusPictureView = {
       let stat = StatusPictureView()
        stat.pictureViewDelegate  = self
        return stat
    }()
    
    /// 底部工具条
     lazy var footerView: StatusTableViewBottomView = StatusTableViewBottomView()
    
    
}

extension StatusTableViewCell:StatusPictureViewDelegate {
    
    func statusPictureView(pictureView: StatusPictureView, didSelected: NSIndexPath, lagreUrl: [NSURL]) {
        cellDelegate?.statusTableViewCell(cell: self, index: didSelected.item, picUrl: lagreUrl)
    }
    
}

